package com.things8.iot.common.result;

import com.things8.iot.common.enums.GlobalResultEnum;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/5/9 21:40
 */

@Getter
@Setter
@NoArgsConstructor
public class GlobalResult<T> {

    private String msg;
    private int code;
    private T data;

    private void setResultEnum(GlobalResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
    }

    public static <T> GlobalResult<T> success(T data) {
        GlobalResult<T> tGlobalResult = new GlobalResult<>();
        tGlobalResult.setData(data);
        tGlobalResult.setResultEnum(GlobalResultEnum.SUCCESS);
        return tGlobalResult;
    }

    public static <T> GlobalResult<T> fail() {
        GlobalResult<T> tGlobalResult = new GlobalResult<>();
        tGlobalResult.setResultEnum(GlobalResultEnum.FAIL);
        return tGlobalResult;
    }

    public static <T> GlobalResult<T> fail(int code, String msg) {
        GlobalResult<T> tGlobalResult = new GlobalResult<>();
        tGlobalResult.setCode(code);
        tGlobalResult.setMsg(msg);
        return tGlobalResult;
    }

}
